package com.heima.xxlJob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Lijx
 * @Date 2024-08-06 10:45
 */
@Component
@Slf4j
@Configuration
public class XxlJobQuickstart {

    @Value("${xxl.job.executor.port}")
    private int port;

    @XxlJob("demoJobHandler")
    public void helloJob(){
        log.info("快速入门，简单任务执行了。。。。");

        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();

        List<Integer> userIds = getUserIds();

        for (int i = 0; i < userIds.size(); i++) {

            if (i % shardTotal == shardIndex) {
                System.out.println(port+"当前第"+shardIndex+"分片执行了，任务项为："+userIds.get(i)+"用户");
            }
        }
        System.out.println();
    }

    //获取用户id列表
    public List<Integer> getUserIds(){
        List<Integer> userIds = new ArrayList<>();

        for (int i = 1; i <= 10; i++) {
            userIds.add(i);
        }

        return userIds;
    }
}
